package com.hxd.charitysystem.ui.activity

import android.app.Dialog
import android.support.v7.widget.LinearLayoutManager
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.hjq.dialog.PayPasswordDialog
import com.hjq.dialog.WaitDialog
import com.hxd.charitysystem.R
import com.hxd.charitysystem.adapter.DetailsImageAdapter
import com.hxd.charitysystem.base.common.MyActivity
import com.hxd.charitysystem.bean.HomeCardItemListBean
import com.hxd.charitysystem.bean.NetBaseBean
import com.hxd.charitysystem.image.ImageLoader
import com.hxd.charitysystem.utils.L
import com.hxd.charitysystem.utils.ProjectUrl
import com.hxd.tabactivityfragment.util.GsonToBean
import com.kongzue.dialog.v3.InputDialog
import com.kymjs.rxvolley.client.HttpCallback
import com.kymjs.rxvolley.client.HttpParams
import com.vondear.rxtool.RxSPTool
import kotlinx.android.synthetic.main.activity_details.*
import org.jetbrains.anko.startActivity

class DetailsActivity : MyActivity() {

    private var adatper: DetailsImageAdapter? = null
    private var projectId: String? = null
    private val token by lazy { RxSPTool.getString(this, "token") }
    private val transferWaitDialog by lazy { WaitDialog.Builder(this).setMessage("正在捐款中...").show(); }

    override fun getLayoutId(): Int {
        return R.layout.activity_details
    }

    override fun getTitleId(): Int {
        return R.id.tb_details_title
    }

    override fun initView() {

        /**
         * 立即捐款按钮
         */
        btn_commit_details.setOnClickListener {
            if (token != "") {
                InputDialog.show(this, "捐款", "请输入捐赠金额", "确定", "取消")
                    .setOnOkButtonClickListener { baseDialog, v, inputStr ->
                        PayPasswordDialog.Builder(this)
                            .setTitle("请输入支付密码")
                            .setSubTitle("捐赠金额")
                            .setMoney(inputStr.toString())
                            .setAutoDismiss(true) // 设置点击按钮后不关闭对话框
                            .setListener(object : PayPasswordDialog.OnListener {
                                override fun onCompleted(dialog: Dialog, password: String) {
                                    /**
                                     * 暂时不用验证支付密码
                                     */

                                    transfer(inputStr.toString().toInt())
                                }

                                override fun onCancel(dialog: Dialog) {
                                    toast("取消了")
                                }
                            })
                            .show()
                        false
                    }
            }else{
                toast("请先登陆后再进行操作")
                startActivity<LoginActivity>()
                finish()
            }
        }
    }

    override fun initData() {
        initCardData()
    }

    /**
     * 初始化首页传递过来的数据
     */
    private fun initCardData() {
        val intent = intent.getStringExtra("singleCard")
        val gson = Gson()
        val type = object : TypeToken<HomeCardItemListBean.CardItem>() {}.type
        val singleData = gson.fromJson<HomeCardItemListBean.CardItem>(intent, type)
        if (singleData.userHeader != "") {
            ImageLoader.loadCircleImage(iv_userPhoto_details, ProjectUrl.getUserImage + singleData.userHeader)
        }
        tv_userName_details.text = singleData.username
        tv_title_details.text = singleData.title
        tv_targetAmount_details.text = singleData.targetAmount.toString()
        tv_raisedAmount_details.text = singleData.raisedAmount.toString()
        tv_helpCount_details.text = singleData.helpCount.toString()
        tv_desc_details.text = singleData.desc
        projectId = singleData.projectId
        if (singleData.assistantImages != null) {
            adatper = DetailsImageAdapter(singleData.assistantImages)
            recycle_image_details.adapter = adatper
            val layoutManager = LinearLayoutManager(this)
            layoutManager.orientation = LinearLayoutManager.HORIZONTAL
            recycle_image_details.layoutManager = layoutManager
        }
    }

    /**
     * 捐款操作
     */
    private fun transfer(amount: Int) {
        val params = HttpParams()
        params.putHeaders("token", token)
        params.put("amount", amount)
        params.put("projectId", projectId)
        transferWaitDialog
        ProjectUrl.postTransfer(params, object : HttpCallback() {
            override fun onSuccess(result: String?) {
                val data = GsonToBean.JsonToBean<NetBaseBean<String>>(result)
                transferWaitDialog.dismiss()
                if (data.status == 200) {

                    toast("捐款成功")
                    finish()
                } else {
                    transferWaitDialog.dismiss()
                    toast("捐款失败:${data.msg}")
                    L.i("捐款失败 ${data.msg}")
                }
            }

            override fun onFailure(errorNo: Int, strMsg: String?) {
                transferWaitDialog.dismiss()
                L.i(" 捐款失败 : $errorNo, strMsg: $strMsg ")
            }
        })
    }
}
